package com.sh.controller;

import com.sh.common.BaseResult;
import com.sh.common.exception.BizException;
import com.sh.pojo.bo.*;
import com.sh.service.user.IUserResourceService;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.simpleframework.xml.core.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 资源对象-写（校验权限）
 */
@Slf4j
@RestController
@RequestMapping("/user/resource/write")
public class UserWriteResourceController {
    @Autowired
    private IUserResourceService userResourceService;

    /**
     * 上传文件
     */
    @PostMapping("/object/upload")
    public BaseResult upload(@RequestParam("file") MultipartFile file, @RequestParam("bucket") String bucket, @RequestParam("objectName") String objectName, @RequestParam(value = "path", required = false) String path) {
        String errMsg;
        try {
            userResourceService.uploadObject(file, bucket, path, objectName);
            return BaseResult.success();
        } catch (Exception e) {
            log.error("上传对象失败：{}", e);
            errMsg = e.getMessage();
        }
        return BaseResult.fail(errMsg);
    }

    /**
     * 上传文件夹
     * */
    @PostMapping(value = "/folder/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public BaseResult uploadFolder(@RequestParam("bucket") String bucket, @RequestParam(value = "path", required = false) @Pattern(regexp = ".*/$", message = "路径需要以'/'结束") String path,  @RequestParam("files") List<MultipartFile> files) {
        String errMsg;
        try {
            userResourceService.uploadFolder(bucket, path, files);
            return BaseResult.success();
        } catch (Exception e) {
            log.error("上传文件夹失败：{}", e);
            errMsg = e.getMessage();
        }
        return BaseResult.fail(errMsg);
    }

    /**
     * 新建文件夹
     */
    @PostMapping("/folder/create")
    public BaseResult createFolder(@Validate @RequestBody CreateFolderBO bo) {
        String errMsg;
        try {
            userResourceService.createFolder(bo.getBucket(), bo.getPath(), bo.getFolderName());
            return BaseResult.success();
        } catch (Exception e) {
            log.error("创建文件夹失败：{}", e);
            errMsg = e.getMessage();
        }
        return BaseResult.fail(errMsg);
    }


    /**
     * 删除
     */
    @PostMapping("/object/delete")
    public BaseResult deleteObject(@Validate @RequestBody DeleteObjectBO bo) throws BizException {
        userResourceService.deleteObject(bo.getBucket(), bo.getPath(), bo.getObjectName());
        return BaseResult.success();
    }

    /**
     * 批量删除
     * */
    @PostMapping("/objects/delete")
    public BaseResult deleteObjects(@Validate @RequestBody DeleteObjectsBO bo) throws BizException {
        userResourceService.deleteObjects(bo.getBucket(),bo.getObjectPath());
        return BaseResult.success();
    }

    /**
     * 删除文件夹
     * */
    @PostMapping("/folder/delete")
    public BaseResult deleteFolder(@Validate @RequestBody DeleteFolderBO bo) throws BizException {
        userResourceService.deleteFolder(bo.getBucket(), bo.getFolderPath());
        return BaseResult.success();
    }

    /**
     * 创建副本
     * */
    @PostMapping("/object/copy")
    public BaseResult copyObject(@Validate @RequestBody CopyObjectBO bo) throws BizException {
        userResourceService.copyObject(bo.getBucket(), bo.getPath(), bo.getObjectName(),bo.getTargetObjectName());
        return BaseResult.success();
    }

    /**
     * 批量创建文件/文件夹副本
     * */
    @PostMapping("/objects/copy")
    public BaseResult copyObjects(@Validate @RequestBody CopyObjectsBO bo){
        userResourceService.copyObjects(bo);
        return BaseResult.success();
    }

    /**
     * 重命名文件
     */
    @PostMapping("/object/rename")
    public BaseResult renameObject(@Validate @RequestBody RenameObjectBO bo) throws BizException {
        userResourceService.renameObject(bo.getBucket(), bo.getPath(), bo.getOldObjectName(), bo.getNewObjectName());
        return BaseResult.success();
    }

    /**
     * 重命名文件夹
     */
    @PostMapping("/folder/rename")
    public BaseResult renameFolder(@Validate @RequestBody RenameFolderBO bo) throws BizException {
        userResourceService.renameFolder(bo.getBucket(), bo.getPath(), bo.getOldFolderName(), bo.getNewFolderName());
        return BaseResult.success();
    }


}
